local map = vim.keymap.set

-- <leader>b

-- <leader>bb
map("n", "<leader>bb", "<cmd>e #<cr>", { desc = "Switch to Other Buffer" })

-- <leader>bd
map("n", "<leader>bd", function()
	Snacks.bufdelete()
end, { desc = "Delete Buffer" })

-- <leader>bD
map("n", "<leader>bD", "<cmd>:bd<cr>", { desc = "Delete Buffer and Window" })

-- <leader>bo
map("n", "<leader>bo", function()
	Snacks.bufdelete.other()
end, { desc = "Delete Other Buffers" })

-- <leader>c

-- <leader>cd
map("n", "<leader>cd", vim.diagnostic.open_float, { desc = "Line Diagnostics" })

-- <leader>cf
map({ "n", "x" }, "<leader>cf", function()
	-- formatting
	LazyVim.format({ force = true })
end, { desc = "Format" })

-- <leader>d

-- <leader>dpp
Snacks.toggle.profiler():map("<leader>dpp")
-- <leader>dph
Snacks.toggle.profiler_highlights():map("<leader>dph")

-- <leader>f

-- <leader>fn
map("n", "<leader>fn", "<cmd>enew<cr>", {
	-- new file
	desc = "New File",
})

-- <leader>ft
map("n", "<leader>ft", function()
	Snacks.terminal(nil, { cwd = LazyVim.root() })
end, { desc = "Terminal (Root Dir)" })

-- <leader>fT
map("n", "<leader>fT", function()
	-- floating terminal
	Snacks.terminal()
end, { desc = "Terminal (cwd)" })

-- <leader>g

-- <leader>gb
map("n", "<leader>gb", function()
	Snacks.picker.git_log_line()
end, { desc = "Git Blame Line" })

-- <leader>gB
map({ "n", "x" }, "<leader>gB", function()
	Snacks.gitbrowse()
end, { desc = "Git Browse (open)" })

-- <leader>gf
map("n", "<leader>gf", function()
	Snacks.picker.git_log_file()
end, { desc = "Git Current File History" })

if vim.fn.executable("lazygit") == 1 then
	-- <leader>gg
	map("n", "<leader>gg", function()
		-- lazygit
		Snacks.lazygit({ cwd = LazyVim.root.git() })
	end, { desc = "Lazygit (Root Dir)" })

	-- <leader>gG
	map("n", "<leader>gG", function()
		Snacks.lazygit()
	end, { desc = "Lazygit (cwd)" })
end

-- <leader>gl
map("n", "<leader>gl", function()
	Snacks.picker.git_log({ cwd = LazyVim.root.git() })
end, { desc = "Git Log" })

-- <leader>gL
map("n", "<leader>gL", function()
	Snacks.picker.git_log()
end, { desc = "Git Log (cwd)" })

-- <leader>gY
map({ "n", "x" }, "<leader>gY", function()
	Snacks.gitbrowse({
		open = function(url)
			vim.fn.setreg("+", url)
		end,
		notify = false,
	})
end, { desc = "Git Browse (copy)" })

-- <leader>K
map("n", "<leader>K", "<cmd>norm! K<cr>", {
	--keywordprg
	desc = "Keywordprg",
})

-- <leader>l
map("n", "<leader>l", "<cmd>Lazy<cr>", {
	-- lazy
	desc = "Lazy",
})

-- <leader>L
map("n", "<leader>L", function()
	LazyVim.news.changelog()
end, {
	-- LazyVim Changelog
	desc = "LazyVim Changelog",
})

-- <leader>u

-- <leader>ua
Snacks.toggle.animate():map("<leader>ua")

-- <leader>uA
Snacks.toggle
	.option("showtabline", {
		off = 0,
		on = vim.o.showtabline > 0 and vim.o.showtabline or 2,
		name = "Tabline",
	})
	:map("<leader>uA")

-- <leader>ub
Snacks.toggle.option("background", { off = "light", on = "dark", name = "Dark Background" }):map("<leader>ub")

-- <leader>uc
Snacks.toggle
	.option("conceallevel", {
		off = 0,
		on = vim.o.conceallevel > 0 and vim.o.conceallevel or 2,
		name = "Conceal Level",
	})
	:map("<leader>uc")

-- <leader>ud
Snacks.toggle.diagnostics():map("<leader>ud")

-- <leader>uD
Snacks.toggle.dim():map("<leader>uD")

-- <leader>uf
LazyVim.format.snacks_toggle():map("<leader>uf")

-- <leader>uF
LazyVim.format.snacks_toggle(true):map("<leader>uF")

-- <leader>ug
Snacks.toggle.indent():map("<leader>ug")

-- <leader>uh
if vim.lsp.inlay_hint then
	Snacks.toggle.inlay_hints():map("<leader>uh")
end

-- <leader>ul
Snacks.toggle.line_number():map("<leader>ul")

-- <leader>uL
Snacks.toggle.option("relativenumber", { name = "Relative Number" }):map("<leader>uL")

-- <leader>ur
map("n", "<leader>ur", "<Cmd>nohlsearch<Bar>diffupdate<Bar>normal! <C-L><CR>", {
	-- Clear search, diff update and redraw
	-- taken from runtime/lua/_editor.lua
	desc = "Redraw / Clear hlsearch / Diff Update",
})

-- <leader>us
Snacks.toggle.option("spell", { name = "Spelling" }):map("<leader>us")

-- <leader>uS
Snacks.toggle.scroll():map("<leader>uS")

-- <leader>uT
Snacks.toggle.treesitter():map("<leader>uT")

-- <leader>uw
Snacks.toggle.option("wrap", { name = "Wrap" }):map("<leader>uw")
